# -*- coding: utf-8 -*-

from openerp import fields, models, api
from datetime import datetime , timedelta

class month_report(models.Model):
    _name = 'month.report'

    plan_qty=fields.Float(string=u"计划完成量")

class week_report(models.Model):
    _name = 'week.report'

    week_th=fields.Char(string=u"第几周")
    start_date=fields.Datetime(string=u"从某年月日")
    end_date=fields.Datetime(string=u"到某年月日")
    plan_qikuai=fields.Float(string="计划砌块发货")
    qikuai_ok=fields.Float(string=u"完成%")
    qikuai_out=fields.Float(string=u"砌块发货")
    B_out=fields.Float(string=u"含等外品")
    ave_qikuai_qty=fields.Float(string=u"日发量")
    plan_bancai=fields.Float(string=u"计划板材发货")
    bancai_ok=fields.Float(string=u"板材完成%")
    bancai_out=fields.Float(string=u"板材发货")
    ave_bancai_qty=fields.Float(string=u"板材日发量")
    money_plan=fields.Float(string=u"计划货款回收")
    money_ok=fields.Float(string=u"货款完成%")
    money_qty=fields.Float(string=u"货款回收")
    money_B=fields.Float(string=u"B类等外品货款")
    tech_work_1=fields.Text(string=u"工程技术部工作")
    sale_work_1=fields.Text(string=u"销售工作")
    market_work_1=fields.Text(string=u"市场部工作")
    product_1=fields.Float(string=u"产成品发货(m³)")
    work_1=fields.Text(string=u"主要工作思路")
    qikuai_1=fields.Float(string=u"计划下周砌块发货(m³)")
    bancai_1=fields.Float(string=u"计划下周板材发货(m³)")
    money_2=fields.Float(string=u"计划货款回收(万)")
    work_2=fields.Text(string=u"主要工作思路")
    tech_work_2=fields.Text(string=u"工程技术部工作")
    sale_work_2=fields.Text(string=u"销售工作")
    market_work_2=fields.Text(string=u"市场部工作")

    # @api.model
    # def create(self, vals):
    #     result = super(week_report, self).create(vals)
    #     self.get_week_start_end(datetime.now())
    #     result._count_out_qty()

    # @api.model
    # def get_week_start_end(self,today):
    #     start_d = -today.weekday() -1
    #     end_d = 5 - today.weekday()
    #     self.stare_date = fields.Datetime.to_string(today + timedelta(days=start_d))[0:10]
    #     self.end_date = fields.Datetime.to_string(today + timedelta(days=end_d))[0:10]

    @api.one
    def _count_out_qty(self):
        #砌块的发货记录查询
        qikuai_stock_ids = self.env['stock.move'].search([
            ('product_id.categ_id','child_of',['2']),
            ('product_id.categ_id.name','=','砌块'),
            ('date','&lt;=',self.end_date),
            ('date','&gt;=',self.stare_date),
            ('location_dest_id.usage','=','customer')
            ])
        print('111111111111111')
        print(qikuai_stock_ids)
        #砌块的发货数量计算
        self.qikuai_out=sum(qikuai_line.product_uom_qty for qikuai_line in qikuai_stock_ids)
        #砌块的发货完成率计算
        if self.plan_qikuai and self.plan_qikuai!=0:
            self.qikuai_ok=self.qikuai_out / self.plan_qikuai
            self.ave_qikuai_qty=self.qikuai_ok / 7
        #砌块B类等外品的发货记录查询
        product_id_b = self.env['product.product'].search([("attribute_value_ids","ilike","B类等外品")],limite=1)


        B_stock_ids = self.env['stock.move'].search([
            ('product_id.categ_id','child_of',['2']),
            ('product_id.categ_id.name','=','砌块'),
            ('product_id.attribute_value_ids','ilike','B类等外品'),
            ('date','&lt;=',self.end_date),
            ('date','&gt;=',self.stare_date),
            ('location_dest_id.usage','=','customer')
            ])
        print(B_stock_ids)
        #砌块B类等外品的发货数量统计
        self.B_out=sum(B_line.product_uom_qty for B_line in B_stock_ids)
        #板材的发货记录查询
        bancai_stock_ids = self.env['stock.move'].search([
            ('product_id.categ_id','child_of',['2']),
            ('product_id.categ_id.name','=','板材'),
            ('date','&lt;=',self.end_date),
            ('date','&gt;=',self.stare_date),
            ('location_dest_id.usage','=','customer')
            ])
        print(bancai_stock_ids)
        #板材的发货数量计算
        self.bancai_out=sum(bancai_line.product_uom_qty for bancai_line in bancai_stock_ids)
        #板材的发货完成率计算
        if self.plan_bancai and self.plan_bancai!=0:
            self.bancai_ok=self.bancai_out / self.plan_bancai
            self.ave_qikuai_qty=self.bancai_ok / 7

        #货款的回收数量统计
        money_ids = self.env['account.invoice.line'].search([
            ('product_id.categ_id','child_of',['2']),
            ('create_date','&lt;=',self.end_date),
            ('create_date','&gt;=',self.stare_date)])
        self.money_qty=sum(money_line.price_subtotal for momey_line in money_ids)
        if self.money_plan and self.money_plan!=0:
            money_ok=self.meoney_qty / self.money_plan
        #B类等外品的回收数量统计
        moneyB_ids = self.env['account.invoice.line'].search([
            ('product_id.categ_id','child_of',['2']),
            ('product_id.attribute_2','ilike',u'B类等外品'),
            ('create_date','&lt;=',self.end_date),
            ('create_date','&gt;=',self.stare_date)])
        self.money_B=sum(moneyB_line.price_subtotal for momeyB_line in moneyB_ids)